package com.cltech.frame.result;


import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.domain.Page;

import java.io.Serializable;
import java.util.Collection;

/**
 * Created by Administrator on 2019/3/6.
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
@Getter
@Setter
public class PageResult<T> implements Serializable {
    private Integer pageNum;
    //每页的数量
    private Integer pageSize;
    //总记录数
    private Long total;
    //总页数
    private Integer totalPages;
    //结果集
    private Collection<T> list;

    public static <T> PageResult<T> result(Long total, Collection<T> rows) {
        return new PageResult<>(total, rows);
    }
    public static <T> PageResult<T> result(Page<T> page) {
        return new PageResult<>(page);
    }
    public PageResult(Integer pageNum, Integer pageSize, Long total, Integer totalPages, Collection<T> list) {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.total = total;
        this.totalPages = totalPages;
        this.list = list;
    }

    public PageResult(Long total, Collection<T> list) {
        this.total = total;
        this.list = list;
    }
    public PageResult(Page<T> page) {
        this.total = page.getTotalElements();
        this.list = page.getContent();
    }
}
